<?php 
    session_start();
//    echo "Login: ".$_SESSION["userID"]." ".$_SESSION["userType"];
?>
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        // put your code here
        if(isset($_SESSION["userID"]) && !strcmp($_SESSION["userType"], "seller")){        
        
        $button = $_POST["button"];
        $productID = $_POST["productID"];
        $categoryID = $_POST["categoryID"];
        $productName = $_POST["productName"];
        $productDescription = $_POST["productDescription"];
        $productImage = $_FILES["productImage"]["name"];
        $productPrice = $_POST["productPrice"];
        
        if(strcmp($button, "Cancel")){
        $con = mysql_connect("localhost", "zhouzhao", "19831022");
        if(!$con){
            die("could not connect to DB: ".mysql_error());
        }
        
        mysql_selectdb("cloudcom", $con);
        
        if(strlen($productID) == 0){
            mysql_query("insert into products values
            (null, '$categoryID', '$productName', '$productDescription', '$productImage', $productPrice)");
//            echo "<p style='color:red'>new product $productName is inserted</p>";
        }else{
            mysql_query("update products set categoryID='$categoryID', productName='$productName', productDescription='$productDescription', productImage='$productImage', productPrice=$productPrice where productID=$productID");
//            echo "<p style='color:red'>product $productID is updated</p>";
        }
        
        mysql_close($con);
        
        if(($_FILES["productImage"]["type"] == "image/gif" ||
                $_FILES["productImage"]["type"] == "image/jpeg" ||
                $_FILES["productImage"]["type"] == "image/pjpeg") ||
                $_FILES["productImage"]["type"] == "image/png" &&
                ($_FILES["productImage"]["size"]) <= 100000){
            if($_FILES["productImage"]["error"] > 0){
                echo "error: ".$_FILES["productImage"]["error"]."<br/>";
            }else{
//                echo "uploaded: ".$_FILES["productImage"]["name"]."<br/>";
//                echo "type: ".$_FILES["productImage"]["type"]."<br/>";
//                echo "size: ".($_FILES["productImage"]["size"]/1024)." Kb<br/>";
//                echo "store: ".$_FILES["productImage"]["tmp_name"];
                
                if(file_exists("images/products/".$_FILES["productImage"]["name"])){
//                    echo $_FILES["productImage"]["name"]."  already exist";
                }else{
                    move_uploaded_file($_FILES["productImage"]["tmp_name"], 
                            "images/products/".$_FILES["productImage"]["name"]);
                }
            }
                }else{
                    echo "invalid file";
                }
        
        require 'seller.php';
        }else{
            require 'seller.php';
        }
        
        }else{
            require 'login.html';
        }
        ?>
    </body>
</html>
